/*
Copyright (C) 2011 Salil Bhagurkar

This file is part of illusion

illusion is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

illusion is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public License
along with illusion.  If not, see <http://www.gnu.org/licenses/>.
*/

#include <klib/string.h>

uint_t strlen(const char *c)
{
	uint_t len = 0;
	while((*c) != '\0') {
		len++;
		c++;
	}
	return len;
}

bool streq(const char *s1, const char *s2)
{
	while(1) {
		if((*s1) != (*s2))
			return 0;
		if((*s1) == '\0' && (*s2) == '\0')
			return true;
		else if((*s1) == '\0' || (*s2) == '\0')
			return false;
		s1++;
		s2++;
	}
}

bool strneq(const char *s1, const char *s2, uint_t count)
{
	while(count--) {
		if((*s1) != (*s2))
			return false;
		if((*s1) == '\0' && (*s2) == '\0')
			return true;
		else if((*s1) == '\0' || (*s2) == '\0')
			return false;
		s1++;
		s2++;
	}
	return 0;
}

char *strcpy(char *dest, const char *src, uint_t max_len)
{
	if (max_len == 0)
		return dest;
	char * const dest_orig = dest;
	char c;
	while (--max_len && (c = *src) != '\0')
	{
		*dest++ = c;
		++src;
	}
	*dest = '\0';
	return dest_orig;
}

char *strcat(char *dst, uint_t dst_buf_len, const char *src)
{
	char *dst_max = dst + dst_buf_len;
	dst += strlen(dst);
	while((*src) != '\0' && dst < (dst_max - 1)) {
		*dst++ = *src++;
	}
	*dst = '\0';
	return dst;
}

bool strstartswith(const char *str, const char *with)
{
	while((*with) != '\0') {
		if((*str) != (*with))
			return 0;
		str++;
		with++;
	}
	return true;
}
